package kmi;

import java.awt.Color;
import static org.junit.Assert.assertTrue;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that GreenMachine fire a
 * bullet after it shoots an enemy.
 * 
 * @author Mitchell Kupfer
 */

public class TestOnBulletHit extends RobotTestBed {

  int hitEnemy = 0;
  int colorChoice = Color.black.getRGB();
  int secondaryHit = 0;
  boolean test = false;

  /**
   * Match between GreenMachine and sitting duck.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,kmi.GreenMachine";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * count number of times we shoot and hit an enemy and the number of times we then shoot them with
   * our secondary onBulletHit method.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    // Get the bullets on the field each turn
    IBulletSnapshot[] bullet = event.getTurnSnapshot().getBullets();

    for (int i = 0; i < bullet.length; i++) {

      if (bullet[i].getVictimIndex() == 0) {
        hitEnemy++;
      }

      if (bullet[i].getColor() == colorChoice && 
          bullet[i].getVictimIndex() == 0 && 
          bullet[i].getOwnerIndex() == 1) {
        secondaryHit++;
      }

    }

  }

  /**
   * After the battle, check to see if we generally shot a black bullet for every time we shot and
   * hit enemy.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {

    // if the difference between the number of times we
    // shot an enemy and the number of times we shot
    // them with a black bullet is relatively small.
    // will never be equal because of different robot
    // behaviors such as ramming etc..

    if (hitEnemy / secondaryHit < 4) {
      test = true;
    }

    assertTrue("Secondary shot works", test);

  }

}
